package com.song.files.controller;

import com.song.api.BaseController;
import com.song.api.controller.files.TestFileControllerApi;
import com.song.grace.result.GraceJSONResult;
import com.song.grace.result.ResponseStatusEnum;
import com.song.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class TestFileController extends BaseController implements TestFileControllerApi {

    private static final Logger LOG = LoggerFactory.getLogger(TestFileController.class);

    @Override
    public GraceJSONResult testFile(MultipartFile multipartfile) throws IOException {

        if (multipartfile.isEmpty() || multipartfile == null) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_FAILD);
        }

        // 获取文件名
        String fileName = multipartfile.getOriginalFilename();
        System.out.println("fileName = " + fileName);
        // 获取文件流，定义存放服务器临时文件位置和名称
//        String tempPath = "/workspaces/temp_face";
        File fileTemp = new File(TEMP_PATH);
        if (!fileTemp.exists()) {
            fileTemp.mkdirs();
        }

        File myFile = new File(TEMP_PATH + "/" + fileName);
        multipartfile.transferTo(myFile);
        if (myFile == null) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_FAILD);
        }
        System.out.println(myFile.getAbsolutePath());

        // 1.转换人脸数据为base64
        String base64Face = FileUtils.fileToBase64(myFile);
        System.out.println(base64Face);
        return GraceJSONResult.ok(base64Face);
    }
}
